package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 开锁日志
func (*DataParsing) parsingBankLog(catData []string, ftpInfoId int64) []model.DataBank {
	var logDate, LogTime, steamId, gameName, gameId, accountCardNum, dataType, eventData, free string
	var location, eventType, content, transactionAmount, processedAmount, accountGameId, accountGameName, transferCardNum, transferGameName, transferGameId, balance, AccountTxt string
	var end int
	var dataArr []model.DataBank

	for _, logTxt := range catData {
		//fmt.Println("原始数据", logTxt)
		accountCardNum = ""
		location = ""
		eventType = ""
		content = ""
		transactionAmount = ""
		processedAmount = ""
		accountGameId = ""
		accountGameName = ""
		transferCardNum = ""
		transferGameName = ""
		transferGameId = ""
		balance = ""
		AccountTxt = ""
		eventData = ""
		dataType = ""
		free = ""
		parts := strings.Split(logTxt, ":")
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//开锁类型过滤
		start := strings.Index(logTxt, ": [")
		if start < 0 {
			dataType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], "]")
			if end < 0 {
				dataType = ""
			} else {
				dataType = logTxt[start+3 : start+3+end]
			}
		}

		if dataType != "Bank" {
			//不是银行数据跳过
			continue
		}

		//获取steamId
		/*start = strings.Index(logTxt, ", ")
		//fmt.Println("位置1", start)
		if start < 0 {
			steamId = ""
		} else {
			end = strings.Index(logTxt[start+2:], ")")
			if end < 0 {
				steamId = ""
			} else {
				steamId = logTxt[start+2 : start+2+end]
			}
		}*/

		//fmt.Println("steamid", steamId)

		//游戏名名称获取
		start = strings.Index(logTxt, "] ")
		//fmt.Println("位置1", start)
		if start < 0 {
			gameName = ""
		} else {
			end = strings.Index(logTxt[+start+2:], "(")
			if end < 0 {
				gameName = ""
			} else {
				gameName = logTxt[+start+2 : start+2+end]
				gameName = TruncateStringByCharCount(gameName, 50)

				// 输出字节集（以十六进制表示）
				//fmt.Printf("Byte slice: %x\n", byteSlice)

				// 将字节集转换回字符串
				gameName = filterInvalidChars(gameName)
			}
		}

		//游戏名id获取
		start = strings.Index(logTxt, "ID:")
		if start < 0 {
			steamId = ""
			//fmt.Println("找不到单引号")
			//return
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], ")")
			if end < 0 {
				steamId = ""
				//fmt.Println("找不到第二个单引号")
				//return
			} else {
				// 提取引号内的数据
				steamId = logTxt[start+3 : start+3+end]
			}
		}

		// 卡号
		start = strings.Index(logTxt, "ber:")
		if start < 0 {
			accountCardNum = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+4:], ")")
			if end < 0 {
				accountCardNum = ""
			} else {
				accountCardNum = logTxt[start+4 : start+4+end]
			}
		}

		// 操作类型
		start = strings.Index(logTxt, ") ")
		if start < 0 {
			eventData = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+2:], "at ")
			if end < 0 {
				eventData = ""
			} else {
				eventData = logTxt[start+2 : start+2+end]
			}
		}

		//数据事件类型
		if strings.Contains(logTxt, "cancelled") {
			eventType = "cancelled"
			//取消卡

			//获取卡类型
			//这里的空格和上面的方法不一样
			start = strings.Index(logTxt, "cancelled ")
			if start < 0 {
				content = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+9:], " card at")
				if end < 0 {
					content = ""
				} else {
					content = logTxt[start+9 : start+9+end]
				}
			}

		} else if strings.Contains(logTxt, "transferred") {
			//转账

			eventType = "transferred"
			//fmt.Println(logTxt)
			//操作金额
			start = strings.Index(logTxt, "ferred ")
			if start < 0 {
				transactionAmount = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+7:], "(")
				if end < 0 {
					transactionAmount = ""
				} else {
					transactionAmount = logTxt[start+7 : start+7+end]
				}
			}

			//截取文本
			start = strings.Index(logTxt, "transferred")
			if start < 0 {
				AccountTxt = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+11:], "from")
				if end < 0 {
					AccountTxt = ""
				} else {
					AccountTxt = logTxt[start+11 : start+11+end]
				}
			}

			//处理金额
			start = strings.Index(AccountTxt, "(")
			if start < 0 {
				processedAmount = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(AccountTxt[start+1:], " was")
				if end < 0 {
					processedAmount = ""
				} else {
					processedAmount = AccountTxt[start+1 : start+1+end]
				}
			}

			//截取转账数据
			start = strings.Index(logTxt, "from Account Number")
			if start < 0 {
				AccountTxt = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+19:], "to")
				if end < 0 {
					AccountTxt = ""
				} else {
					AccountTxt = logTxt[start+19 : start+19+end]
				}
			}

			//转账卡所属的卡号
			start = strings.Index(AccountTxt, ": ")
			if start < 0 {
				accountCardNum = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(AccountTxt[start+2:], "(")
				if end < 0 {
					accountCardNum = ""
				} else {
					accountCardNum = AccountTxt[start+2 : start+2+end]
				}
			}

			//转账卡所属的游戏id
			start = strings.Index(AccountTxt, "(")
			if start < 0 {
				accountGameName = ""
			} else {
				// 查找第二个单引号的位置    上面卡号位置 + 卡号 + 括号
				end = strings.Index(AccountTxt[start+1:], ")(")
				if end < 0 {
					accountGameName = ""
				} else {
					accountGameName = AccountTxt[start+1 : start+1+end]
				}
			}

			//转账卡所属的游戏id
			start = strings.Index(AccountTxt, ")(")
			if start < 0 {
				accountGameId = ""
			} else {
				// 查找第二个单引号的位置    上面卡号位置 + 卡号 + 括号
				end = strings.Index(AccountTxt[start+2:], ")")
				if end < 0 {
					accountGameId = ""
				} else {
					accountGameId = AccountTxt[start+2 : start+2+end]
				}
			}

			//截取到账数据
			start = strings.Index(logTxt, "to Account Number")
			if start < 0 {
				AccountTxt = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+17:], "at")
				if end < 0 {
					AccountTxt = ""
				} else {
					AccountTxt = logTxt[start+17 : start+17+end]
				}
			}

			//到账所属的卡号
			start = strings.Index(AccountTxt, ": ")
			if start < 0 {
				transferCardNum = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(AccountTxt[start+2:], "(")
				if end < 0 {
					transferCardNum = ""
				} else {
					transferCardNum = AccountTxt[start+2 : start+2+end]
				}
			}

			//到账所属的游戏id
			start = strings.Index(AccountTxt, "(")
			if start < 0 {
				transferGameName = ""
			} else {
				// 查找第二个单引号的位置    上面卡号位置 + 卡号 + 括号
				end = strings.Index(AccountTxt[start+1:], ")(")
				if end < 0 {
					transferGameName = ""
				} else {
					transferGameName = AccountTxt[start+1 : start+1+end]
				}
			}

			//到账所属的游戏id
			start = strings.Index(AccountTxt, ")(")
			if start < 0 {
				transferGameId = ""
			} else {
				// 查找第二个单引号的位置    上面卡号位置 + 卡号 + 括号
				end = strings.Index(AccountTxt[start+2:], ")")
				if end < 0 {
					transferGameId = ""
				} else {
					transferGameId = AccountTxt[start+2 : start+2+end]
				}
			}

		} else if strings.Contains(logTxt, "purchased") {
			//买卡
			eventType = "purchased"
			//这里的空格和上面的方法不一样
			start = strings.Index(logTxt, "purchased ")
			if start < 0 {
				content = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+10:], " card")
				if end < 0 {
					content = ""
				} else {
					content = logTxt[start+9 : start+10+end]
				}
			}
			//买卡付费状态
			start = strings.Index(logTxt, "wal: ")
			if start < 0 {
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+5:], ")")
				if end < 0 {
				} else {
					free = logTxt[start+5 : start+5+end]
					content = content + ":" + free
				}
			}

			start = strings.Index(logTxt, "balance is ")
			if start < 0 {
				balance = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+11:], " credits")
				if end < 0 {
					balance = ""
				} else {
					balance = logTxt[start+11 : start+11+end]
				}
			}
			//其他
		} else if strings.Contains(eventData, "manually destroyed") {
			//销毁卡
			eventType = "manually destroyed"

			//销毁卡类型
			start = strings.Index(logTxt, "destroyed ")
			if start < 0 {
				content = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+10:], " card")
				if end < 0 {
					content = ""
				} else {
					content = logTxt[start+10 : start+10+end]
				}
			}

			start = strings.Index(logTxt, "to Account Number")
			if start < 0 {
				AccountTxt = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+18:], "at")
				if end < 0 {
					AccountTxt = ""
				} else {
					AccountTxt = logTxt[start+18 : start+18+end]
				}
			}

			start = strings.Index(AccountTxt, ":")
			if start < 0 {
				accountCardNum = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(AccountTxt[start+1:], ",")
				if end < 0 {
					accountCardNum = ""
				} else {
					accountCardNum = AccountTxt[start+1 : start+1+end]
				}
			}

			//其他
		} else if strings.Contains(logTxt, "deposited") {
			//存钱
			eventType = "deposited"
			//fmt.Println(logTxt)
			//操作金额
			start = strings.Index(logTxt, "deposited ")
			if start < 0 {
				transactionAmount = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+10:], "(")
				if end < 0 {
					transactionAmount = ""
				} else {
					transactionAmount = logTxt[start+10 : start+10+end]
				}
			}

			//start = strings.Index(logTxt, "deposited")
			if start < 0 {
				AccountTxt = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+9:], "to")
				if end < 0 {
					AccountTxt = ""
				} else {
					AccountTxt = logTxt[start+9 : start+9+end]
				}
			}

			//处理金额
			start = strings.Index(AccountTxt, "(")
			if start < 0 {
				processedAmount = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(AccountTxt[start+1:], " was")
				if end < 0 {
					processedAmount = ""
				} else {
					processedAmount = AccountTxt[start+1 : start+1+end]
				}
			}

			//截取转账数据
			start = strings.Index(logTxt, "to Account Number")
			if start < 0 {
				AccountTxt = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+17:], "at")
				if end < 0 {
					AccountTxt = ""
				} else {
					AccountTxt = logTxt[start+17 : start+17+end]
				}
			}

			//转账卡所属的卡号
			start = strings.Index(AccountTxt, ": ")
			if start < 0 {
				accountCardNum = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(AccountTxt[start+2:], "(")
				if end < 0 {
					accountCardNum = ""
				} else {
					accountCardNum = AccountTxt[start+2 : start+2+end]
				}
			}

			//转账卡所属的游戏id
			start = strings.Index(AccountTxt, "(")
			if start < 0 {
				accountGameName = ""
			} else {
				// 查找第二个单引号的位置    上面卡号位置 + 卡号 + 括号
				end = strings.Index(AccountTxt[start+1:], ")(")
				if end < 0 {
					accountGameName = ""
				} else {
					accountGameName = AccountTxt[start+1 : start+1+end]

					accountGameName = TruncateStringByCharCount(accountGameName, 50)

					// 输出字节集（以十六进制表示）
					//fmt.Printf("Byte slice: %x\n", byteSlice)

					// 将字节集转换回字符串
					accountGameName = filterInvalidChars(accountGameName)
				}
			}

			//转账卡所属的游戏id
			start = strings.Index(AccountTxt, ")(")
			if start < 0 {
				accountGameId = ""
			} else {
				// 查找第二个单引号的位置    上面卡号位置 + 卡号 + 括号
				end = strings.Index(AccountTxt[start+2:], ")")
				if end < 0 {
					accountGameId = ""
				} else {
					accountGameId = AccountTxt[start+2 : start+2+end]
				}
			}
			//其他
		} else if strings.Contains(logTxt, "withdrew") {
			//取钱
			eventType = "withdrew"

			//操作金额
			start = strings.Index(logTxt, "withdrew ")
			if start < 0 {
				transactionAmount = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+9:], "(")
				if end < 0 {
					transactionAmount = ""
				} else {
					transactionAmount = logTxt[start+9 : start+9+end]
				}
			}
			//截取文本
			start = strings.Index(logTxt, "withdrew")
			if start < 0 {
				AccountTxt = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+11:], "from")
				if end < 0 {
					AccountTxt = ""
				} else {
					AccountTxt = logTxt[start+11 : start+11+end]
				}
			}
			//处理金额
			start = strings.Index(AccountTxt, "(")
			if start < 0 {
				processedAmount = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(AccountTxt[start+1:], " was")
				if end < 0 {
					processedAmount = ""
				} else {
					processedAmount = AccountTxt[start+1 : start+1+end]
				}
			}

			//截取转账数据
			start = strings.Index(logTxt, "from Account Number")
			if start < 0 {
				AccountTxt = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+19:], "at")
				if end < 0 {
					AccountTxt = ""
				} else {
					AccountTxt = logTxt[start+19 : start+19+end]
				}
			}

			//转账卡所属的卡号
			start = strings.Index(AccountTxt, ": ")
			if start < 0 {
				accountCardNum = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(AccountTxt[start+2:], "(")
				if end < 0 {
					accountCardNum = ""
				} else {
					accountCardNum = AccountTxt[start+2 : start+2+end]
				}
			}

			//转账卡所属的游戏id
			start = strings.Index(AccountTxt, "(")
			if start < 0 {
				accountGameName = ""
			} else {
				// 查找第二个单引号的位置    上面卡号位置 + 卡号 + 括号
				end = strings.Index(AccountTxt[start+1:], ")(")
				if end < 0 {
					accountGameName = ""
				} else {
					accountGameName = AccountTxt[start+1 : start+1+end]
					accountGameName = TruncateStringByCharCount(accountGameName, 50)

					// 输出字节集（以十六进制表示）
					//fmt.Printf("Byte slice: %x\n", byteSlice)

					// 将字节集转换回字符串
					accountGameName = filterInvalidChars(accountGameName)
				}
			}

			//转账卡所属的游戏id
			start = strings.Index(AccountTxt, ")(")
			if start < 0 {
				accountGameId = ""
			} else {
				// 查找第二个单引号的位置    上面卡号位置 + 卡号 + 括号
				end = strings.Index(AccountTxt[start+2:], ")")
				if end < 0 {
					accountGameId = ""
				} else {
					accountGameId = AccountTxt[start+2 : start+2+end]
				}
			}

		}

		start = strings.Index(logTxt, "at X")
		if start < 0 {
			location = ""
		} else {
			// 查找第二个单引号的位置
			location = logTxt[start+3:]
		}
		//判断去除地址最后的.
		length := len(location)
		if location[length-1] == '.' {
			// 使用切片操作去掉最后一个字符
			location = location[:length-1]
		}

		//fmt.Println("数据为：", ftpInfoId, logDate, Time, LogTime, steamId, gameName, gameId, loginType, loginIp, location)

		//gameId, _ := strconv.ParseInt(gameId, 10, 64)
		//fmt.Println(eventType, processedAmount)
		downloadFile := model.DataBank{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime, SteamId: steamId, GameName: gameName, GameId: gameId,
			EventType: eventType, Balance: balance, TransactionAmount: transactionAmount, ProcessedAmount: processedAmount,
			AccountCardNum: accountCardNum, AccountGameName: accountGameName, AccountGameId: accountGameId,
			TransferCardNum: transferCardNum, TransferGameName: transferGameName, TransferGameId: transferGameId, Content: content, Location: location}

		dataArr = append(dataArr, downloadFile)
	}
	//panic("停")
	return dataArr
}
